from typing import Sequence

from fastapi import APIRouter, UploadFile, Security
from fastapi.responses import FileResponse, StreamingResponse

from app.deps.authorize_deps import validate_permissions
from app.deps.base_deps import DynamicQueryDep, IdsQueryDep
from app.deps.controller_deps import FileControllerDep as ControllerDep
from app.models.base import IdType
from app.schemas.base_schema import AuthScopePrefixConst
from app.schemas.file_schema import FileOut as OutModel
from app.schemas.result_schema import ResultModel, PageModel

file_router = APIRouter()

SCOPE_PREFIX=AuthScopePrefixConst.FILE_SCOPE_PREFIX.value


@file_router.get("/get_all", summary="获取所有",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_all(query_in: DynamicQueryDep, controller: ControllerDep) -> ResultModel[Sequence[OutModel]]:
    return await controller.get_all( query_in=query_in)

@file_router.get("/get_page/{current_page}/{page_size}", summary="分页获取",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_page(current_page: int, page_size: int, query_in: DynamicQueryDep, controller: ControllerDep) -> ResultModel[PageModel[OutModel]]:
    return await controller.get_page(current_page=current_page, page_size=page_size, query_in=query_in,)

@file_router.get("/get_by_id/{id}", summary="根据ID获取",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_by_id(id: IdType,controller: ControllerDep) -> ResultModel[OutModel]:
    return await controller.get_by_id(id=id)

@file_router.get("/get_by_ids", summary="根据ID列表获取",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_by_ids(ids:IdsQueryDep,controller: ControllerDep) -> ResultModel[Sequence[OutModel]]:
    return await controller.get_by_ids(ids=ids)

@file_router.post("/upload_file", summary="上传文件",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}upload"])])
async def upload_file(file: UploadFile,controller: ControllerDep) -> ResultModel[OutModel]:
    return await controller.upload_file(file=file)

@file_router.post("/upload_files", summary="批量上传文件",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}upload"])])
async def upload_files(files: Sequence[UploadFile],controller: ControllerDep) -> ResultModel[Sequence[OutModel]]:
    return await controller.upload_files(files=files)

@file_router.post("/download_file/{id}", summary="下载文件",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}download"])])
async def download_file(id: IdType,controller: ControllerDep) -> FileResponse:
    return await controller.download_file(id=id)

@file_router.post("/download_files", summary="批量下载文件",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}download"])])
async def download_files(ids: Sequence[IdType] ,controller: ControllerDep) -> StreamingResponse:
    return await controller.download_files(ids=ids)

@file_router.delete("/delete/{id}", summary="删除",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}delete"])])
async def delete(id: IdType,controller: ControllerDep) -> ResultModel[bool]:
    return await controller.delete(id=id)

@file_router.delete("/batch_delete", summary="批量删除",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}batch_delete"])])
async def batch_delete(ids: Sequence[IdType],controller: ControllerDep) -> ResultModel[bool]:
    return await controller.batch_delete(ids=ids)
